<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul id="list">
        <li class="li">0</li>
        <li class="li">1</li>
        <li class="li">2</li>
        <li class="li">3</li>
    </ul>
    <script>
        let ul = document.getElementById('list').children;
        for (let i = 0; i < ul.length; i++) {
            ul[i].setAttribute('index', i);
            ul[i].onclick = function() {
                alert(ul[i].getAttribute('index'))
            }
        }
    </script>
</body>

</html>